package com.github.thinwonton.spring.service.impl;

import com.github.thinwonton.spring.entity.User;
import com.github.thinwonton.spring.service.UserService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * Created by hugo on 2017/6/8.
 */
@Service
@CacheConfig(cacheNames = {"userCache"})
public class UserServiceImpl implements UserService {

//    @Cacheable(value = "userCache#(60 * 10)",key = "'user:'+#id", condition = "#result != null")
    @Cacheable(key = "'user:'+#id", unless = "#result == null")
    @Override
    public User getById(long id) {
        System.out.println("getById():" + id);
        return new User(id, "hugo");
    }

    @CacheEvict(key = "'user:'+#id")
    @Override
    public void deleteById(long id) {
        System.out.println("deleteById():" + id);
        throw new RuntimeException("deleteById()方法发生了异常");
    }

    @CacheEvict(key = "'user:'+#user.id")
    @Override
    public void update(User user) {
        System.out.println("update():" + user.toString());
    }
}
